package kuonjisai.swing;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class SwingPopupMenu extends JPopupMenu {
	

	private JPopupMenu popup = null;
	
	private JMenu pmnEdit = null;
	private JMenu pmnTool = null;
	
	
	public SwingPopupMenu( JPanel p ) {
		// 팝업메뉴 생성 메서드 호출 pop-4
		popup = new JPopupMenu();
		
		pmnEdit = new JMenu("설 정                 " );
		popup.add( pmnEdit );			// ** 팝업메뉴에 Jmenu 추가 * 중요
		makeMenuItem();
		pmnTool = new JMenu("도 구" );
		popup.add( pmnTool );
		
		
		p.addMouseListener( new PopupMenuListener( ) );
		
		
	// SwingMain의 contentPane(JPanel)에
	//popupmenulistener를
	// mouseListener로 등록
	
		
	}
	
	
	
	
	private JMenuItem makeMenuItems( String text, boolean off ) {
		JMenuItem mnitem = null;
		
		mnitem = new JMenuItem(text); 
		mnitem.setEnabled(off);
		return mnitem;
		
	}
	
	
	private void makeMenuItem() {
		pmnEdit.add( makeMenuItems("열 기        ", true) );
		pmnEdit.add( makeMenuItems("닫 기", true) );
		
	}
	
	class PopupMenuListener extends MouseAdapter {
		
			public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		private void showMenu(MouseEvent e) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}


	}


	
}


/*class PopupMenuListener extends MouseAdapter {
	
	private JPopupMenu popup = null;
	// -3 인자생성 
	
	public PopupMenuListener( JPopupMenu popup ) {
			this.popup = popup;
	}		// pop -2 생성자 생성후 Jpopup으로 받음
	
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger()) {
			showMenu(e);
		}
	}
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger()) {
			showMenu(e);
		}
	}
	private void showMenu(MouseEvent e) {
		popup.show(e.getComponent(), e.getX(), e.getY());
	}


}
*/ // 관련 코드들은 같은 메뉴안에 넣는게 좋을수도 있다.

